<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="menuQuery">
        <el-form-item>
          <el-input v-model="menuQuery.keyword" placeholder="关键字"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="getMenu">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
        <el-table :data="pageInfo.list" highlight-current-row v-loading="listLoading" @selection-change="selsChange"
                  style="width: 100%;">
          <el-table-column type="selection" width="55">
          </el-table-column>
          <el-table-column prop="id" label="ID" width="60">
          </el-table-column>
          <el-table-column prop="name" label="部门名称" width="120" sortable>
          </el-table-column>
          <el-table-column prop="url" label="菜单路径" width="120" sortable>
          </el-table-column>
          <el-table-column prop="parent.name" label="父菜菜单" width="120" sortable>
          </el-table-column>
          <el-table-column prop="icon" label="图标" width="80" sortable>
            <template slot-scope="scope">
              <i :class="scope.row.icon"></i>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="150">
            <template scope="scope">
              <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
              <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="menuQuery.currentPage"
          :page-sizes="[3, 5, 10, 20]"
          :page-size="menuQuery.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pageInfo.total"
          style="float:right;">
      </el-pagination>
    </el-col>

    <!--编辑/新增界面-->
    <el-dialog title="编辑/新增" :visible.sync="saveFormVisible" :close-on-click-modal="false">
      <el-form :model="saveForm" label-width="80px" :rules="saveFormRules" ref="editForm">
        <el-form-item label="菜单名称" prop="name">
          <el-input v-model="saveForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="菜单路径" prop="url">
          <el-input v-model="saveForm.url" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="菜单图标" prop="icon">
          <el-select v-model="saveForm.icon" clearable value-key="id" placeholder="请选择">
            <el-option
                v-for="item in icons"
                :key="item.id"
                :label="item.icon"
                :value="item.icon">
              <span style="float: left"><i :class="item.icon"></i></span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.name }}</span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="父菜单" prop="parent">
          <el-select v-model="saveForm.parent" clearable value-key="id" placeholder="请选择">
            <el-option v-if="saveForm.id !== item.id"
                       v-for="item in menus"
                       :key="item.id"
                       :label="item.name"
                       :value="item">
              <span>{{ item.name }}</span>
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="saveFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
      </div>
    </el-dialog>
  </section>
</template>

<script>
export default {
  data() {
    return {
      // 分页信息
      pageInfo: {
        total: 0,
        list: []
      },
      // 高级查询
      menuQuery: {
        keyword: null,
        currentPage: 1,
        pageSize: 10
      },

      listLoading: false,
      sels: [],//列表选中列

      saveFormVisible: false,//编辑界面是否显示
      editLoading: false, // 编辑页面是否提交中
      saveFormRules: { // 表单验证规则
        name: [
          {required: true, message: '请输入姓名', trigger: 'blur'}
        ]
      },
      //编辑界面数据
      saveForm: {
        id: null,
        name: '',
        url: '',
        icon: '',
        parent: {
          id: null
        }
      },
      icons: [
        {id: 1, icon: 'el-icon-bangzhu', name: '权限'},
        {id: 2, icon: 'el-icon-s-home', name: '主页'},
        {id: 3, icon: 'el-icon-message', name: '消息'},
        {id: 4, icon: 'el-icon-s-custom', name: '员工'},
        {id: 5, icon: 'el-icon-menu', name: '菜单'},
        {id: 6, icon: 'el-icon-set-up', name: '组织'},
        {id: 7, icon: 'el-icon-s-platform', name: '部门'},
        {id: 8, icon: 'el-icon-s-cooperation', name: '权限管理'},
        {id: 9, icon: 'el-icon-user', name: '角色'},
      ],
      // 上级部门
      menuTree: [],
      // 父菜单
      menus: []
    }
  },
  methods: {
    // 获得父级菜单
    getMenus() {
      this.$http.get("/menu/parent")
          .then(result => {
            this.menus = result.data.data;
          })
          .catch(error => {
            this.$message({
              message: error.data.msg,
              type: 'error'
            });
          })
    },
    // 分页事件
    handleCurrentChange(val) {
      this.menuQuery.currentPage = val;
      this.getMenu();
    },
    handleSizeChange(val) {
      this.menuQuery.pageSize = val;
      this.getMenu();
    },
    //获取部门列表
    getMenu() {
      this.$http.post("/menu", this.menuQuery)
          .then(result => {
            this.pageInfo = result.data.data;
          })
          .catch(error => {
            this.$message({
              message: error.data.msg,
              type: 'error'
            });
          })
    },
    //删除
    handleDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        //NProgress.start();
        this.$http.delete("/menu/" + row.id)
            .then(res => {
              res = res.data
              if (res.success) {
                this.$message({
                  message: '删除成功',
                  type: 'success'
                });
                this.listLoading = false;
                this.menuQuery.currentPage = 1;
                this.getMenu();
              } else {
                this.$message({
                  message: '删除失败',
                  type: 'error'
                });
              }
            })
            .catch(res => {
              this.$message({
                message: res.data.msg,
                type: 'error'
              });
            })
      }).catch(() => {

      });
    },
    //显示编辑界面
    handleEdit: function (index, row) {
      this.saveFormVisible = true;
      this.getMenus();
      this.saveForm = Object.assign({}, row);
      // this.$refs['editForm'].resetFields('name');
    },
    //显示新增界面
    handleAdd: function () {
      this.saveFormVisible = true;
      this.getMenus();
      this.saveForm = {
        id: null,
        name: '',
        url: '',
        icon: '',
        parent: {
          id: null
        }
      };
      // this.$refs['editForm'].resetFields();
    },
    //编辑&新增
    editSubmit: function () {
      this.$refs.editForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.editLoading = true;
            var saveForm = this.saveForm;
            if (Object.is(saveForm.parent, "")) {
              saveForm.parent = {
                id: null
              }
            }
            this.$http.put("/menu", saveForm)
                .then(res => {
                  res = res.data;
                  if (res.success) {
                    this.$message({
                      message: '提交成功',
                      type: 'success'
                    });
                    this.saveFormVisible = false;
                    this.editLoading = false;
                    this.menuQuery.currentPage = 1;
                    this.getMenu();
                  } else {
                    this.$message({
                      message: '提交失败',
                      type: 'error'
                    });
                  }
                })
                .catch(res => {
                  this.$message({
                    message: res.data.msg,
                    type: 'error'
                  });
                });
          });
        }
      });
    },
    selsChange: function (sels) {
      // sels.forEach(item => {
      //   this.$refs.multipleTable.toggleRowSelection(item);
      // });
      // this.$refs.multipleTable.toggleRowSelection(row);
      this.sels = sels;
    },
    //批量删除
    batchRemove: function () {
      var ids = this.sels.map(item => item.id);

      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        this.$http.patch("/menu", ids)
            .then(res => {
              res = res.data;
              if (res.success) {
                this.listLoading = false;
                this.$message({
                  message: '删除成功',
                  type: 'success'
                });
                this.menuQuery.currentPage = 1;
                this.getMenu();
              } else {
                this.$message({
                  message: '删除失败',
                  type: 'error'
                });
              }
            })
            .catch(res => {
              this.$message({
                message: res.data.msg,
                type: 'error'
              });
            });
      }).catch(() => {

      });
    }
  },
  mounted() {
    this.getMenu();
  }
}

</script>

<style scoped>

</style>